package com.niiwoo.civet.trade.service.local.newRefund.refundDeal.core;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.account.dto.request.TransUnfreezeRequestDTO;
import com.niiwoo.civet.account.dto.response.TransUnfreezeResponseDTO;
import com.niiwoo.civet.account.enums.TransUnfreezeStatusEnum;
import com.niiwoo.civet.account.service.trade.TransOrderDubboService;
import com.niiwoo.civet.base.enums.CommonResultEnum;
import com.niiwoo.civet.trade.dao.entity.RefundOrder;
import com.niiwoo.civet.trade.dao.mapper.RefundOrderMapperExt;
import com.niiwoo.civet.trade.dto.common.RefundUnfreezeResultMsgDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 解冻
 */
@Service
@Slf4j
public class UnFreezeDealingService {

    @Reference(version = "1.0.0")
    private TransOrderDubboService transOrderDubboService;

    @Autowired
    private RefundOrderMapperExt refundOrderMapperExt;

    /**
     * 解冻批处理
     */
    public void unFreezeDealBatch(){
        log.info("还款解冻补单批处理start");
        List<RefundOrder> list = refundOrderMapperExt.selectUnFreezeDealingBatch();
        UnFreezeDealingService unFreezeDealingService = (UnFreezeDealingService)AopContext.currentProxy();
        for (RefundOrder refundOrder : list) {
            try {
                unFreezeDealingService.unFreezeDeal(refundOrder.getId());
            } catch (Exception e) {
                log.error("还款查询解冻处理失败，refundOrderId={}", refundOrder.getId(), e);
            }
        }
        log.info("还款解冻补单批处理end");
    }

    /**
     * 解冻处理
     * @param refundOrderId
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public void unFreezeDeal(Long refundOrderId){
        RefundOrder refundOrder = refundOrderMapperExt.selectByPrimaryKeyForUpdate(refundOrderId);
        // 只有处理中才需要去解冻
        if(Byte.valueOf("1").equals(refundOrder.getUnfreezeStatus())){
            TransUnfreezeRequestDTO requestDTO = new TransUnfreezeRequestDTO();
            requestDTO.setFreezeRequestNo(refundOrder.getFreezeRequestNo());
            requestDTO.setAmount(refundOrder.getAmount());
            TransUnfreezeResponseDTO transUnfreezeResponseDTO = transOrderDubboService.transUnfreeze(requestDTO);
            if(TransUnfreezeStatusEnum.SUCCESS.equals(transUnfreezeResponseDTO.getTransUnfreezeStatusEnum())){
                // 解冻完成，整个订单结束
                RefundOrder refundOrderUpdater = new RefundOrder();
                refundOrderUpdater.setId(refundOrderId);
                refundOrderUpdater.setUnfreezeStatus(Byte.valueOf("2"));
                refundOrderUpdater.setProcessStatus(Byte.valueOf("2"));
                refundOrderUpdater.setOverTime(new Date());
                refundOrderUpdater.setUpdateTime(new Date());
                refundOrderMapperExt.updateByPrimaryKeySelective(refundOrderUpdater);
            }else if(TransUnfreezeStatusEnum.FAIL.equals(transUnfreezeResponseDTO.getTransUnfreezeStatusEnum())){
                // 改为解冻失败，需要定时任务找出并邮件通知出来
                RefundOrder refundOrderUpdater = new RefundOrder();
                refundOrderUpdater.setId(refundOrderId);
                refundOrderUpdater.setUnfreezeStatus(Byte.valueOf("3"));
                refundOrderUpdater.setUpdateTime(new Date());
                refundOrderMapperExt.updateByPrimaryKeySelective(refundOrderUpdater);
            }
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public void unFreezeDeal(RefundUnfreezeResultMsgDTO resultMsgDTO) {
        Long refundOrderId = resultMsgDTO.getOrderId();
        RefundOrder refundOrder = refundOrderMapperExt.selectByPrimaryKeyForUpdate(refundOrderId);
        // 只有处理中才需要去解冻
        if(Byte.valueOf("1").equals(refundOrder.getUnfreezeStatus())){
            if(CommonResultEnum.SUCCESS.equals(resultMsgDTO.getFreezeResult())){
                // 解冻完成，整个订单结束
                RefundOrder refundOrderUpdater = new RefundOrder();
                refundOrderUpdater.setId(refundOrderId);
                refundOrderUpdater.setUnfreezeStatus(Byte.valueOf("2"));
                refundOrderUpdater.setProcessStatus(Byte.valueOf("2"));
                refundOrderUpdater.setOverTime(new Date());
                refundOrderUpdater.setUpdateTime(new Date());
                refundOrderMapperExt.updateByPrimaryKeySelective(refundOrderUpdater);
            }else if(CommonResultEnum.FAILURE.equals(resultMsgDTO.getFreezeResult())){
                // 改为解冻失败，需要定时任务找出并邮件通知出来
                RefundOrder refundOrderUpdater = new RefundOrder();
                refundOrderUpdater.setId(refundOrderId);
                refundOrderUpdater.setUnfreezeStatus(Byte.valueOf("3"));
                refundOrderUpdater.setUpdateTime(new Date());
                refundOrderMapperExt.updateByPrimaryKeySelective(refundOrderUpdater);
            }
        }
    }
}
